<?php


namespace app\controller;
use app\domain\car;
use app\domain\violation;
use app\server\VioUser;
use function PHPSTORM_META\elementType;
use rap\db\Record;
use rap\web\Request;

class ViolationController{

    public function index() {


        return "Violation/index";
    }

    public function enroll() {


        return "Violation/enroll";
    }

    public function login() {


        return "Violation/login";
    }

    public function ranklist() {


        return "Violation/ranklist";
    }

    public function user() {


        return "Violation/user";
    }

    /**
     * 用户报名入口
     * @param Request $request
     */
    public function addUserInfo(Request $request) {
        //
        if ($request->method() == "POST") {

            $user_id = $request->param('user_id', 3105);
            $user_name = $request->param('user_name', '提提');
            $plate = $request->param('plate', '鲁MC088L');
            $phone = $request->param('phone', '');
            $id_card = $request->param('id_card', '');
            $gender = $request->param('gender', 1);
            $token = $request->param('token', '');
            $img = $request->param('img', "");

//            if (!$token) return CommonController::false(404, '请登录');

            if (!$plate) return CommonController::false(401, '用户车牌号不能为空', []);
            if (!$user_name) return CommonController::false(401, '用户姓名不能为空', []);
            if (!$gender) return CommonController::false(401, '用户性别不能为空', []);
            if (!$phone) return CommonController::false(401, '用户手机号不能为空', []);
            if (!$id_card) return CommonController::false(401, '用户身份证号不能为空', []);
            if (!$img) return CommonController::false(401, '车辆行驶证图片不能为空', []);

            $res = CommonController::checkUserPhone($phone);

            if ($res == 0) return CommonController::false(401, '用户手机号格式不正确', []);

            $common = new CommonController();
            $drive_info = $common->getOcrData($img, "driving");

            $driving_obj = (array)json_decode($drive_info[1]);
            $driving_data = (array)$driving_obj['Data'];
            if (!$driving_data['plate_num']){
                return $common->false(401, '形式正图片不正确', []);
            }
            $data = [
                'user_name' => $user_name,
                'car_num' => $plate,
                'id_card' => $id_card,
                'user_id' => $user_id,
                'user_phone' => $phone,
                'gender' => $gender,
                'user_permit_img' => $img,
                'create_time' => date("Y-m-d H:i:s", time())
            ];

            //添加到数据库
            $res = violation::addUserViolationInfo($data, $drive_info);
            if ($res)return $common->success("报名成功", []);
            else return $common->false(404, '请重新再试', []);
        }
    }

    /**
     * 获取用户排行榜
     * @param Request $request
     */
    public function getUserRankList(Request $request) {

        if ($request->method() == "POST") {
//            $page = $request->param('page', 1);
//
//            $where = [
//                'is_del' => 0,
//            ];
//
//            $field = "";
//
//            $model = new violation();
//
//            $data = $model->addUserViolationInfo($where, $page, $field);\

            $data = [[
                'name' => "张三",
                'car_number' => "鲁M123456"
            ],
                [
                'name' => "张三",
                'car_number' => "鲁M123456"
            ],
                [
                'name' => "张三",
                'car_number' => "鲁M123456"
            ],
                [
                'name' => "张三",
                'car_number' => "鲁M123456"
            ],
                [
                'name' => "张三",
                'car_number' => "鲁M123456"
            ],
                [
                'name' => "张三",
                'car_number' => "鲁M123456"
            ],
            [
                'name' => "张三",
                'car_number' => "鲁M123456"
            ],
            [
                'name' => "张三",
                'car_number' => "鲁M123456"
            ]
            ];

            $common = new CommonController();
            if ($data){
                 return $common->success('获取数据成功', $data);
            }else {
                return $common->false(404, '暂无数据', []);
            }

            return $data;

        }
    }

    /**
     * 获取用户违章信息
     */
    public function getUserPecInfoList(Request $request) {

        if ($request->method() == "POST"){
            $token = $request->param("token", '');
            $car_code = $request->param('car_code', '');
            $engine_code = $request->param('engine_code', '');
            $class_code = $request->param('class_code', '');

            //车牌号判断
            $car_co = substr($car_code, 0, 4);
            if ($car_co != "鲁M"){
                return CommonController::false(404,"暂时只支持滨州地区用户", []);
            }

            $config = Config::getFileConfig();

            $params = array(
                'key' => $config['app_info']['wz_key'],
                'city'  => "SD_BZ",
                'hphm' => $car_code,
                'hpzl' => '02',
                'engineno'=> substr($engine_code, 1, 6),
                'classno'   => substr($class_code,-6)
            );

            $api = new JvheController();

            $data = $api->juhecurl($config['app_info']['wzUrl'], $params, 1);

            if ($data['resultcode'] = "208"){
                return [
                    'code' => 404,
                    'success' => false,
                    'msg' => '您所查询的城市正在维护或未开通查询',
                    'data' => [],
                ];
            }

            $data = (array)json_decode($data)->result;


            $list = $data['lists'];

            if ($list) {
                $res = array();
                for ($i = 0; $i < count($list); $i++) {
                    $res[$i] = (array)$list[$i];
                }
                $res1 = peccancyInfo::addPecInfo($res, $_SESSION[$token]['user_id'], $car_code);
                if ($list) {
                    $info = setScore::getScoreInfo(['isdelete' => 0, 'type_name' => '违章查询'], "score");
                    $where = [
                        'user_id' => $_SESSION[$token]['user_id'],
                        'gold' => $info['score'],
                        'remark' => "违章查询",
                        'secret' => Config::getFileConfig()['app_info']['app_secret'],
                    ];
                    CommonController::modifyGold($where);
                    return [
                        'code' => 200,
                        'success' => true,
                        'msg' => '获取信息成功',
                        'data' => $list,
                    ];
                } else {
                    return [
                        'code' => 404,
                        'success' => false,
                        'msg' => '暂无违章信息',
                        'data' => $list,
                    ];
                }
            } else {
                return [
                    'code' => 404,
                    'success' => false,
                    'msg' => '暂无违章信息',
                    'data' => [],
                ];
            }
        }
    }

    /**
     * 获取用户信息
     * @param Request $request
     */
    public function getUserInfo(Request $request) {
        if ($request->method() == "POST"){

        }
    }

}